package com.common.util.update;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.common.util.R;
import com.common.util.utils.LogUtils;

/**
 * @文件名称：CustomProgressBar.java
 * @文件作者：ljb
 * @文件描述：自定义升级进度条
 * @版本： 1.0.0
 * @创建时间：2017-10-31 17:10
 * @修改历史：2017-10-31 17:10
 */

public class CustomProgressBar extends View {

    // 第一种颜色  进度条颜色
    int mFirstColor;
    // 第二种颜色
    int mSecondColor;
    // 圆环宽度
    int mCircleWidth;
    // 速度
    private int mSpeed;
    // 当前进度
    private int mProgress;
    // 是否开始下一步
    private boolean isNext;
    // 画笔
    private Paint mPaint;
    // 用来开关线程
    private boolean isContinue;

    public CustomProgressBar(Context context) {
        this(context, null);
    }

    public CustomProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    /**
     * 2、在View的构造方法中获得我们自定义的属性
     */
    public CustomProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        final TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomProgressBar, defStyleAttr, 0);
        int indexCount = typedArray.getIndexCount();
        for (int i = 0; i < indexCount; i++) {
            int arr = typedArray.getIndex(i);
            if (arr == R.styleable.CustomProgressBar_firstColor) {
                mFirstColor = typedArray.getColor(arr, Color.BLUE);

            } else if (arr == R.styleable.CustomProgressBar_secondColor) {
                mSecondColor = typedArray.getColor(arr, Color.BLUE);

            } else if (arr == R.styleable.CustomProgressBar_circleWidth) {
                mCircleWidth = typedArray.getDimensionPixelSize(arr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()));

            } else if (arr == R.styleable.CustomProgressBar_speed) {
                mSpeed = typedArray.getInt(arr, 20);

            }
        }

        typedArray.recycle();
        mPaint = new Paint();
        isContinue = true;
        // 绘图线程
//        new Thread() {
//            public void run() {
//                while (isContinue) {
//                    mProgress++;
//                    if (mProgress == 360) {
//                        mProgress = 0;
//                        isNext = !isNext;
//                    }
//                    Log.e("--------", "在执行..");
//                    postInvalidate();
//
//                    try {
//                        Thread.sleep(100 / mSpeed);// 这里优化了一下,值越大,速度越快
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//
//            }
//        }.start();
    }

    public void setProgress(int progress) {
        mProgress = (int) (progress * 3.6);
        invalidate();
    }

    /**
     * 4、重写onDraw
     */
    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
        // 这里使用width主要是用来防止用户使用wrap_content,会占满整个屏幕宽,默认为200dp
        int center = width / 2;// 获取圆心的x坐标         getMeasuredWidth()也行!!!!
        LogUtils.e("------------->", "getWidth():" + center);
        int radius = center - mCircleWidth / 2;// b半径
        mPaint.setStrokeWidth(mCircleWidth);//设置圆环的宽度
        mPaint.setAntiAlias(true);//抗锯齿
        mPaint.setStyle(Paint.Style.STROKE);//设置空心
        // 用于定义圆弧的形状和大小的界限
        RectF rectF = new RectF(center - radius, center - radius, center + radius, center + radius);

        if (!isNext) {// 第一个颜色的圈跑完,第二个颜色跑
            mPaint.setColor(mFirstColor);// 设置圆环的颜色
            canvas.drawCircle(center, center, radius, mPaint);// 画圆环
            mPaint.setColor(mSecondColor);// 设置圆环的颜色
            canvas.drawArc(rectF, -90, mProgress, false, mPaint);// 根据进度画圆弧
        } else {
            mPaint.setColor(mSecondColor);
            canvas.drawCircle(center, center, radius, mPaint);
            mPaint.setColor(mFirstColor);
            canvas.drawArc(rectF, -90, mProgress, false, mPaint);
        }
    }

    // 用来关闭线程
    public void setContinue(boolean aContinue) {
        isContinue = aContinue;
    }

    private int width;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);

        if (modeWidth == MeasureSpec.EXACTLY) {
            width = sizeWidth;
        } else {//默认宽度
            width = (int) getContext().getResources().getDimension(R.dimen.progressbar_width);
        }
        LogUtils.e("------------->", "width:" + width);
        setMeasuredDimension(width, width);
    }
}
